/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012 Oliver Katz */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <IL/il.h>
#include <IL/ilu.h>
#include <IL/ilut.h>

GLuint base;

GLuint ilutGLLoadImage(char *FileName);

void init_font(GLuint base, char *f)
{
	Display *display = XOpenDisplay(0);
	XFontStruct *fs = XLoadQueryFont(display, f);
	int first = fs->min_char_or_byte2;
	int last = fs->max_char_or_byte2;
	glXUseXFont(fs->fid, first, last-first+1, base+first);
	XCloseDisplay(display);
}

void print_font(GLuint base, char *s)
{
	glPushAttrib(GL_LIST_BIT);
	glListBase(base);
	glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *)s);
}

void display()
{
	printf("%ix%i\n", glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glColor3f(1.0, 1.0, 1.0);
	glRectf(-0.9, -0.9, -0.5, -0.5);

	glBegin(GL_LINES);
	glVertex2f(0.9, 0.9);
	glVertex2f(0.5, 0.5);
	glEnd();

	glBegin(GL_POINTS);
	glVertex2f(0.9, -0.9);
	glEnd();

	GLuint img = ilutGLLoadImage("data/kitten.jpg");
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, img);
	glBegin(GL_QUADS);
	glTexCoord2f(0.0, 0.0); glVertex2f(0.0, 0.0);
	glTexCoord2f(1.0, 0.0); glVertex2f(1.0, 0.0);
	glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0);
	glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0);
	glEnd();
	glDeleteTextures(1, &img);

	glColor3f(1.0, 1.0, 1.0);
	glRasterPos2f(0.0, 0.0);
	print_font(base, "hello, world");

	glutSwapBuffers();
}

void keyPress(unsigned char k, int x, int y)
{
	printf("keypress %c (%i)\n", k, k);
	if (k == 27)
		_exit(0);
}

void keyRelease(unsigned char k, int x, int y)
{
	printf("keyrelease %c (%i)\n", k, k);
}

void hover(int x, int y)
{
	printf("hover %ix%i\n", x, y);
}

void drag(int x, int y)
{
	printf("drag %ix%i\n", x, y);
}

void button(int b, int s, int x, int y)
{
	if (s == GLUT_UP)
	{
		printf("buttonrelease %i (%ix%i)\n", b, x, y);
	}
	else if (s == GLUT_DOWN)
	{
		printf("buttonpress %i (%ix%i)\n", b, x, y);
	}
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowSize(200, 200);
	glutCreateWindow("-_-");

	ilInit();
	ilutRenderer(ILUT_OPENGL);
	ilutEnable(ILUT_OPENGL_CONV);

	base = glGenLists(256);
	init_font(base, "fixed");

	glClearColor(0.0, 0.0, 0.0, 1.0);
	glutDisplayFunc(display);
	glutKeyboardFunc(keyPress);
	glutKeyboardUpFunc(keyRelease);
	glutPassiveMotionFunc(hover);
	glutMotionFunc(drag);
	glutMouseFunc(button);

	glutMainLoop();

	return 0;
}

